package 数组;

/**
 * @author TTDB
 * @create 2025/9/14
 */
public class firstMissingPositive {
     public int firstMissingPositive(int[] nums) {
         //找出确实的第一个正数
         int n = nums.length;
         for(int i = 0; i < n; i++){
             while(nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]){
                 swap(nums, i, nums[i] - 1);
             }
         }
         for(int i = 0; i < n; i++){
             if(nums[i] != i + 1){
                 return i + 1;
             }
         }
         return n + 1;
    }
    public void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
